package towerz.states;

import java.io.IOException;
import java.util.Vector;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;

import towerz.component.Button;
import towerz.component.ButtonWheel;
import towerz.component.panel.HutPanel;
import towerz.system.BitmapFont;
import towerz.system.StateManager;
import towerz.system.StringManager;
import towerz.system.Utilities;

public class StateFront extends GameState {
	public StateFront() {
		super(GameState.FRONT, StateManager.instance().getBackgroundImage());
		
		//Initialize
		m_Draw		= true;
		Animate		= true;
		m_Selected 	= 0;
		
		//Create buttons
		createButtons();
		
		//Create images
		try {
			m_LiteImage		= Image.createImage(Utilities.INTERFACE_FOLDER + "/lite.png");
			m_TitleImage	= Image.createImage(Utilities.INTERFACE_FOLDER + "/title.png");
			m_VersionImage	= Image.createImage(Utilities.INTERFACE_FOLDER + "/version.png");
			m_ReturnImage	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText(StringManager.instance().getString("exit"), 0x00000000);
		} catch (IOException ex) {}

		//Initialize animation
		m_AnimationTime	= 0;
		m_TitleY		= -m_TitleImage.getHeight();
		m_ButtonsY		= StateManager.instance().getHeight();
		m_Entering		= false;
		m_Exiting		= false;
	}
	
	protected void createButtons() {
		//Get selected id
		int Selected = 1;
		if (m_Wheel != null) Selected = m_Wheel.getSelectedButton().getID();

		//Is mute?
		String onOff = StateManager.instance().isSoundOff() == true ? "off" : "on";
		
		//Create button wheel
		m_Wheel = new ButtonWheel(5, 2);
		m_Wheel.addButton(1, StringManager.instance().getString("play"));
		m_Wheel.addButton(2, StringManager.instance().getString("levels"));
		m_Wheel.addButton(3, StringManager.instance().getString("survival"));
		m_Wheel.addButton(4, StringManager.instance().getString("highscore"));
		m_Wheel.addButton(5, StringManager.instance().getString("sound") + ": " + StringManager.instance().getString(onOff));
		m_Wheel.addButton(6, StringManager.instance().getString("language") + ": " + StringManager.instance().getLanguage().getName());
		m_Wheel.addButton(7, StringManager.instance().getString("help"));
		m_Wheel.addButton(8, StringManager.instance().getString("credit"));
		m_Wheel.addButton(9, StringManager.instance().getString("exit"));
		
		//Set as selected
		m_Wheel.setSelectedID(Selected);
	}

	public void onEnter(){
		StateManager.instance().getMediaPlayer().load(Utilities.BGM_FOLDER + "/title.mid", "audio/mid", -1);
		StateManager.instance().getMediaPlayer().play();
		
		//Draws
		m_Draw = true; 
		
		//Animate?
		if (Animate) m_Entering = true;
		else {
			m_Entering	= true;
			//m_TitleY	= 40;
			//m_ButtonsY	= m_TitleY + m_TitleImage.getHeight() + 16;
		}
	}

	public void keyPressed(int key) {
		//Super
		super.keyPressed(key);

		//Is right pressed?
		if (key == -7 || key == 35) {
			//Cancel sound
			StateManager.instance().playSFX("/cancel.wav");
			
			//Exit
			m_Active = false;
		}
	}
	
	public void update(long time, int keys) {
		//Super
		super.update(time, keys);
		
		//If entering
		if (m_Entering) {
			//Skip if first time
			if (m_AnimationTime == 0) m_AnimationTime = 1;
			else {			
				//Increase
				m_AnimationTime += time;
				m_Draw			= true;
				m_Wheel.forceDraw();

				//Set
				float Comparison	= (float)m_AnimationTime / (float)ANIMATE_TIME;
				m_LiteX				= StateManager.instance().getWidth() - (int)((StateManager.instance().getWidth() - 258) * Comparison);
				m_TitleY			= -m_TitleImage.getHeight() + (int)((m_TitleImage.getHeight() + 40) * Comparison);
				m_ButtonsY			= StateManager.instance().getHeight() - (int)((StateManager.instance().getHeight() - 120) * Comparison);

				//Check time and fire
				if (m_AnimationTime > ANIMATE_TIME || keys != 0) {
					m_Entering		= false;
					m_AnimationTime = 0;

					//Finalize button
					m_TitleY	= 40;
					m_ButtonsY	= m_TitleY + m_TitleImage.getHeight() + 16;
					m_LiteX		= 258;
				}
			}
		} else if (m_Exiting) {
			//If
			if (m_AnimationTime < 0) {
				//No longer exiting
				m_Exiting		= false;
				m_AnimationTime = 0;
				
				//Parameters
				Vector param = new Vector();
				
				//Based on ID
				switch (m_Wheel.getSelectedButton().getID()) {
				case 1:
					//Go to game state
					param.addElement(new Integer(Utilities.getInt(Utilities.MAX_LEVEL_RMS, 1)));
					StateManager.instance().goTo(GameState.LOADING, param, false);
					break;

				case 2:
					//Go to level selection
					StateManager.instance().goTo(GameState.LEVELS, null, false);
					break;

				case 3:
					//Go to survival
					param.addElement(new Integer(-1));
					StateManager.instance().goTo(GameState.LOADING, param, false);
					break;

				case 4:
					//Go to high score
					param.addElement(new Integer(StateHighScore.SHOW_REQUEST));
					StateManager.instance().goTo(GameState.HIGH_SCORE, param, false);
					break;

				case 7:
					//Go to help
					StateManager.instance().goTo(GameState.HELP, null, false);
					break;

				case 8:
					//Go to credit
					StateManager.instance().goTo(GameState.CREDITS, null, false);
					break;

				case 9:
					//Deactivate state
					m_Active = false;
					break;
				}
			} else {			
				//Increase
				m_Draw = true;
				m_AnimationTime += time;
				m_Wheel.forceDraw();

				//Set
				float Comparison	= (float)m_AnimationTime / (float)ANIMATE_TIME;
				m_TitleY			= 40 - (int)((m_TitleImage.getHeight() + 40) * Comparison);
				m_ButtonsY			= 120 + (int)((StateManager.instance().getHeight() - 120) * Comparison);
				m_LiteX				= 258 + (int)((StateManager.instance().getWidth() - 258) * Comparison);

				//Check time
				if (m_AnimationTime > ANIMATE_TIME || keys != 0) m_AnimationTime = -1;
			}
		} else {
			//Update wheel
			m_Wheel.update(keys);
			
			//Check key
			if ((keys & GameCanvas.UP_PRESSED) != 0 || (keys & GameCanvas.DOWN_PRESSED) != 0) {
				//Cursor sound
				StateManager.instance().playSFX("/cursor.wav");

				//Draw
				m_Draw = true;
				m_Wheel.forceDraw();
			}

			//If button is pressed
			if ((keys & GameCanvas.FIRE_PRESSED) != 0) {				
				//If not third button, pressed
				if (m_Wheel.getSelectedButton().getID() != 5 && m_Wheel.getSelectedButton().getID() != 6) {
					m_Exiting = true;
					m_Wheel.getSelectedButton().setState(Button.STATE_PRESSED);
				}
				//Audio
				else if (m_Wheel.getSelectedButton().getID() == 5) {
					//Toggle sound
					boolean Mute = StateManager.instance().isSoundOff();
					StateManager.instance().setSoundOff(!Mute);

					String onOff = StateManager.instance().isSoundOff() == true ? "off" : "on";
					m_Wheel.getSelectedButton().setText(StringManager.instance().getString("sound") + ": " + StringManager.instance().getString(onOff));

					//Draw
					m_Draw = true;
					m_Wheel.forceDraw();
				}
				//Language
				else if (m_Wheel.getSelectedButton().getID() == 6) {
					//Play sound
					StateManager.instance().playSFX("/select.wav");
					
					//Initialize
					int Index = 0;
					StringManager.Language[] Languages = StringManager.Language.getLanguages();
					
					//Get next language
					for (int i = 0; i < Languages.length; i++)
						if (Languages[i] == StringManager.instance().getLanguage()) Index = i + 1;
					if (Index >= Languages.length) Index = 0;
					
					//Load next language
					StringManager.instance().loadString(Languages[Index]);
					HutPanel.loadText();
					
					//Change buttons
					createButtons();
					m_ReturnImage = BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText(StringManager.instance().getString("exit"), 0x00000000);

					//Save preferred language to RMS
					Utilities.putInt(Utilities.PREF_LANGUAGE_RMS, Index);

					//Draw
					m_Draw = true;
					m_Wheel.forceDraw();
				}
			}
		}
	}
	
	public void draw(Graphics g) {
		//Skip if not drawn
		if (!m_Draw) return;
		
		//Super
		super.draw(g);
		
		//Draw stuff
		m_Wheel.setPosition(StateManager.instance().getWidth() / 2, m_ButtonsY);
		m_Wheel.draw(g);
		g.drawImage(m_TitleImage, StateManager.instance().getWidth() / 2, m_TitleY, Graphics.HCENTER | Graphics.TOP);
		g.drawImage(m_LiteImage, m_LiteX, 102, Graphics.TOP | Graphics.LEFT);
		
		//If not animating
		if (!m_Entering && !m_Exiting) {
			g.drawImage(m_VersionImage, StateManager.instance().getWidth(), 0, Graphics.RIGHT | Graphics.TOP);
			g.drawImage(m_ReturnImage, StateManager.instance().getWidth() - 8, StateManager.instance().getHeight()-4, Graphics.RIGHT | Graphics.BOTTOM);
		}
			
		//Don't draw
		m_Draw = false;
		
	}
	
	protected final long ANIMATE_TIME = 500;
	
	//
	public static boolean Animate;

	//Data
	protected int			m_Selected;
	protected boolean		m_Draw;
	protected ButtonWheel	m_Wheel;
	
	//Animation
	protected int		m_LiteX;
	protected int		m_TitleY;
	protected int		m_ButtonsY;
	protected boolean	m_Entering;
	protected boolean	m_Exiting;
	protected int		m_AnimationTime;
	
	//Images
	protected Image m_LiteImage;
	protected Image	m_TitleImage;
	protected Image	m_ReturnImage;
	protected Image m_VersionImage;
}
